<?php

class ImageController extends ActionController
{
    /**
     * 添加图片
    */
    public function actionAdd() {
        if (Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest) {
            $post_data = array(
                'title' => Yii::app()->request->getPost('title'),
                'file_sign' => Yii::app()->request->getPost('file_sign'),
                'type' => Yii::app()->request->getPost('type'),
                'note' => Yii::app()->request->getPost('title', ''),
            );
            $flash = new Image();
            $is_success = $flash->imageAdd($post_data);
            if ($is_success === true) {
                $this->success('添加成功');
            }
            $this->error($flash->getError('add'));
        } else {
            $this->layout = '/layouts/common';
            $type = Yii::app()->request->getQuery('type'); //类型.img图片，video，
            $type_map = Image::$type_map;  //资源类型
            $type_name = isset($type_map[$type]) ? $type_map[$type] : '';  //类型名称

            //图片上传参数
            if (isset(Yii::app()->params['upload'][$type]['size'])) {
                $size = Yii::app()->params['upload'][$type]['size'];
            } else {
                $size = '1M';
            }

            if (isset(Yii::app()->params['upload'][$type]['directory'])) {
                $upload_dir = Yii::app()->params['upload'][$type]['directory'];
            } else {
                $upload_dir = 'upload';
            }

            $render_params = array(
                'type_name' => $type_name,
                'type' => $type,
                'size' => File::humanReadableToBytes($size),
                'upload_url' => Yii::app()->baseUrl . '/' . $upload_dir
            );
            $this->render('add', $render_params);
        }
    }

    /**
     * 上传图片
    */
    public function actionUpload() {
        if (!Yii::app()->request->isAjaxRequest || !Yii::app()->request->isPostRequest) {
            return;
        }
        $pid = Yii::app()->request->getQuery('pid');
        if (empty($pid)) {
            $this->error('参数非法');
        }
        $floder = Floder::model()->findByPk($pid);
        if ($floder === null) {
            $this->error('相册不存在');
        }
        $image = new Image();
        $is_success = $image->imageAdd($pid);
        if ($is_success === false) {
            $this->error($image->getErrors());
        }
        $this->success('添加成功');
    }

    /**
     * 获取图片列表数据
    */
    public function actionList() {
        $pid = Yii::app()->request->getParam('pid');
        if (empty($pid)) {
            $this->error('参数非法');
        }
        if (Yii::app()->request->isAjaxRequest) {
            $image = Image::model();
            $image->pid = $pid;
            $limit = intval(Yii::app()->request->getPost('limit', 12));
            $page = intval(Yii::app()->request->getPost('page', 1));  //默认获取第一页数据
            $page_list = $image->getPageList($page, $limit);
            if ($page_list === false) {
                $this->error($image->getErrors());
            }
            $this->jsonResponse($page_list);
        } else {
            $sub_floder = Floder::model()->findByPk($pid);   //子目录
            if ($sub_floder === null) {
                $this->error('子相册不存在');
            }
            //获取1级目录
            $parent_floder = Floder::model()->findByPk($sub_floder->pid);
            if ($sub_floder === null) {
                $this->error('主相册不存在');
            }
            $allow_ext = Yii::app()->params['upload']['img']['allowExtensions'];
            $view_params = array(
                'pid' => $pid,
                'sub_floder' => $sub_floder,
                'parent_floder' => $parent_floder,
                'allow_ext' => $allow_ext
            );
            $this->render('list', $view_params);
        }
    }

    /**
     * 批量删除
    */
    public function actionBatchDelete() {
        if (Yii::app()->request->isAjaxRequest) {
            $id_list = Yii::app()->request->getPost('id_list');
            if (empty($id_list)) {
                $this->error('参数非法');
            }
            $image_list = Image::model()->findAllByPk($id_list);
            if (empty($image_list)) {
                $this->error('不存在的记录');
            }
            $total = count($image_list);
            $fail = 0;
            foreach ($image_list as $image) {
                $is_success = $image->remove();
                if (!$is_success) {
                    $fail++;
                }
            }
            $success = $total-$fail;
            $msg = "总共{$total}个,成功{$success}个，失败{$fail}";
            $this->success($msg);
        }
    }

    /**
     * 删除图片
    */
    public function actionRemove() {
        $id = Yii::app()->request->getQuery('id');
        if (empty($id)) {
            $this->error('参数非法');
        }
        $flash = Image::model()->findByPk($id);
        if ($flash === null) {
            $this->error('图片不存在');
        }
        $is_success = $flash->remove();
        if ($is_success === false) {
            $this->error($flash->getError('remove'));
        }
        $this->success('删除成功');
    }

    /**
     * 图片查看
    */
    public function actionView() {
        $id = Yii::app()->request->getQuery('id');
        if (empty($id)) {
            $this->error('参数非法');
        }
        $image = Image::model()->findByPk($id);
        if ($image === null) {
            $this->error('相片不存在');
        }

        $this->render('view', array('image' => $image));
    }

    /**
     * 修改图片
     */
    public function actionModify() {
        $id = Yii::app()->request->getParam('id');
        if (empty($id)) {
            $this->error('参数非法');
        }
        $image = Image::model()->findByPk($id);
        if ($image === null) {
            $this->error('相片不存在');
        }
        $title = Yii::app()->request->getParam('title', '');
        if (!empty($title)) {
            $image->title = $title;
        }
        $image->save();
    }
}